package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.DensityUtils;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.ui.adapter.SelectIconAdapter;
import com.qiantu.phone.ui.adapter.SelectSceneIconAdapter;

import java.util.ArrayList;
import java.util.Arrays;

public class SceneIconSelectActivity extends AppActivity {

    private RecyclerView imageList;
    private SelectSceneIconAdapter mSelectSceneIconAdapter;
    private SelectIconAdapter mSelectIconAdapter;
    private int sceneType;
    private int imageListWidth;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_scene_icon_select;
    }

    @Override
    protected void initView() {
        imageList = findViewById(R.id.image_list);
        imageListWidth = DensityUtils.getScreenWidth(getContext()) - imageList.getPaddingLeft() - imageList.getPaddingRight();
        imageList.setLayoutManager(new GridLayoutManager(getContext(), 3));
    }

    @Override
    protected void initData() {
        sceneType = getIntent().getIntExtra("sceneType", 1);
        String selectIcon = getIntent().getStringExtra("selectIcon");
        if(sceneType == 1){
            setTitle(R.string.scene_icon);
            String[] iconUrls = getResources().getStringArray(R.array.scene_icon);;
            ArrayList<String> iconArray = new ArrayList<>(Arrays.asList(iconUrls));
            mSelectSceneIconAdapter = new SelectSceneIconAdapter(imageListWidth / 3){
                @Override
                public void onSelectChanged(String mSelectedIcon) {
                }
            };
            if(!TextUtils.isEmpty(selectIcon)){
                mSelectSceneIconAdapter.setSelectIcon(selectIcon);
            }
            imageList.setAdapter(mSelectSceneIconAdapter);
            mSelectSceneIconAdapter.setNewInstance(iconArray);
        }else{
            setTitle(R.string.scene_intent_icon);
            String[] iconUrls = AppApplication.getInstance().getSceneIntentIconsByThemeId();
            ArrayList<String> iconArray = new ArrayList<>(Arrays.asList(iconUrls));
            mSelectIconAdapter = new SelectIconAdapter(imageListWidth / 3, SelectIconAdapter.TYPE_SCENE_INTENT_ICON){
                @Override
                public void onSelectChanged(String mSelectedIcon) {
                }
            };
            if(!TextUtils.isEmpty(selectIcon)){
                mSelectIconAdapter.setSelectIcon(selectIcon);
            }
            imageList.setAdapter(mSelectIconAdapter);
            mSelectIconAdapter.setNewInstance(iconArray);
        }
    }

    @Override
    public void onRightClick(View view) {
        String selectIcon;
        if(sceneType == 1){
            selectIcon = mSelectSceneIconAdapter.getSelectIcon();
        }else{
            selectIcon = mSelectIconAdapter.getSelectIcon();
            if(TextUtils.isEmpty(selectIcon)){
                toast(R.string.select_intent_icon_hint);
                return;
            }
        }
        Intent intent = new Intent();
        intent.putExtra("DATA", selectIcon);
        setResult(RESULT_OK, intent);
        finish();
    }

}